# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
from MysqlTools import MysqlTools
import os
import json

class CreateImgFolder:

    def __init__(self, **kwargs):
        self.db = MysqlTools(db_config=kwargs.get("db_config"), type='dict')
        self.up_db = MysqlTools(db_config=kwargs.get("db_config"), type='dict')
        self.s_table = kwargs.get("s_table")
        self.p_table = kwargs.get("p_table")
        self.src_img_path = '/Users/duanmengdi/Desktop/workspace/py3_code/JAC_img/'
        self.dest_img_path = '/Users/duanmengdi/Desktop/workspace/py3_code/JAC_img_clean_new/'
        self.X = 10
        self.Y = 10

    def get_all_cnm(self):
        sql = '''select distinct `cid`,`num`,`mid` from `{}`'''.format(self.s_table)
        data = self.db.select_all(sql)
        return data

    def get_imgPoint(self, cid, num, mid):
        sql = '''select `pnum`,`extra_info` from `{}` where `cid`="{}" and `num`="{}" and `mid`="{}";'''.format(self.p_table, cid, num, mid)
        data = self.db.select_all(sql)
        return data

    def calculate_imgPoint(self, imgPoint):
        # {"x":123, "y":234}
        # {"hsX": 278, "hsY": 462, "maxX": 333, "maxY": 478, "hsKey": "11810"}
        _dict = {}
        x = int(imgPoint.get("x"))
        y = int(imgPoint.get("y"))
        _dict["hsX"] = x-self.X
        _dict["hsY"] = y-self.Y
        _dict["maxX"] = x+self.X
        _dict["maxY"] = y+self.Y
        return _dict

    def format_imgPoint(self, imgPoints):
        # {"hsX": 278, "hsY": 462, "maxX": 333, "maxY": 478, "hsKey": "11810"}
        _dict = {"hotspots": [], "imageHeight": 440, "imageWidth": 600, "imageFormat": "image/png", "maxScaleFactor": 1.0}
        for info in imgPoints:
            pnum = info.get("pnum")
            extra_info = json.loads(info.get("extra_info"))
            imgPoint = extra_info.get("imgPoint")
            if not imgPoint:  # 如果没有描点,跳过
                continue
            imgPoint = self.calculate_imgPoint(imgPoint)
            imgPoint['hsKey'] = pnum
            _dict["hotspots"].append(imgPoint)
        return _dict


    def run(self):
        # 取出所有cid, num, mid
        all_cnm = self.get_all_cnm()
        for cnm in all_cnm:
            cid = cnm.get("cid")
            # cid = '3226995_3040'
            num = cnm.get("num")
            # num = '09'
            mid = cnm.get("mid")
            # mid = '8103001V0010'
            print(cid, num, mid)
            # 先创建新的cid路径, 复制原路径图片到新路径
            new_path = self.dest_img_path+cid
            if not os.path.exists(new_path):
                os.mkdir(new_path)
            src_img = self.src_img_path+cid+'/'+mid+'.png'
            if os.path.exists(src_img):
                # 如果图片存在
                dest_img = new_path+'/'+mid.replace("-", "_")+'.png'
                if not os.path.exists(dest_img):
                    command = 'cp "{}" "{}"'.format(src_img, dest_img)
                    os.system(command)

            # cid+num+mid取下边所有pid描点
        #     imgPoints = self.get_imgPoint(cid, num, mid)
        #     # 计算并整合描点
        #     format_imgPoint_dict = self.format_imgPoint(imgPoints)
        #
        #     # 描点计算完集成一个标准描点字典更新到s_table
        #     up_sql = '''update `{}` set `imgPoints`='{}' where `cid`="{}" and `num`="{}" and `mid`="{}"'''.format(self.s_table, json.dumps(format_imgPoint_dict), cid, num, mid)
        #     self.up_db.execute_sql(up_sql)
        # self.up_db.commit_sql()

if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="JAC",
        charset="utf8",
    )
    s_table = 'cars_structure_collection'
    p_table = 'cars_part_num'
    tools = CreateImgFolder(db_config=db_config, s_table=s_table, p_table=p_table)
    tools.run()